package rfBio;

import java.util.StringTokenizer;
import java.util.Vector;

public class psl3 {
	// instance variables
	
	private int match;
	private int mismatch;
	private int repmatch;
	private int ns;
	private int Q_gapCount;
	private int Q_gapBases;
	private int T_gapCount;
	private int T_gapBases;
	private String strand;
	private String Q_name;
	private int Q_size;
	private int Q_start;
	private int Q_end;
	private String T_name;
	private int T_size;
	private int T_start;
	private int T_end;
	private int blockCount;
	private Vector blockSizes = new Vector(10,10);
	private Vector qStarts = new Vector(10,10);
	private Vector tStarts = new Vector(10,10);
	private float percentMatch;
	private int score;
	private boolean overlaps;
	private String Overlaps_With;
	
	// constructors
	public psl3(){
		match = 0;
		mismatch = 0;
		repmatch = 0;
		ns = 0;
		Q_gapCount = 0;
		Q_gapBases = 0;
		T_gapCount = 0;
		T_gapBases = 0;
		strand = "";
		Q_name = "";
		Q_size = 0;
		Q_start = 0;
		Q_end = 0;
		T_name = "";;
		T_size = 0;
		T_start = 0;
		T_end = 0;
		blockCount = 0;
		Vector blockSizes;
		Vector qStarts;
		Vector tStarts;
		percentMatch = 0;
		score = 0;
		overlaps = false;
		Overlaps_With = "";
	}
	
	// copy constructor
	public psl3(psl3 p){
		match = p.match;
		mismatch = p.mismatch;
		repmatch = p.repmatch;
		ns = p.ns;
		Q_gapCount = p.Q_gapCount;
		Q_gapBases = p.Q_gapBases;
		T_gapCount = p.T_gapCount;
		T_gapBases = p.T_gapBases;
		strand = p.strand;
		Q_name = p.Q_name;;
		Q_size = p.Q_size;
		Q_start = p.Q_start;
		Q_end = p.Q_end;
		T_name = p.T_name;
		T_size = p.T_size;
		T_start = p.T_start;
		T_end = p.T_end;
		blockCount = p.blockCount;
		Vector blockSizes = new Vector(p.blockSizes);
		Vector qStarts = new Vector(p.qStarts);
		Vector tStarts = new Vector(p.tStarts);
		percentMatch = p.percentMatch;
		score = p.score;
		overlaps = p.overlaps;
		Overlaps_With = p.Overlaps_With;
	}
	
	// constructor takes a psl3 line
	public psl3(String line){	
		
		String temp_blockSizes = "";
		String temp_qStarts = ""; 
		String temp_tStarts = "";
		int i;
		
		StringTokenizer ltok = new StringTokenizer(line, "\t");
		while ( ltok.hasMoreTokens() ){
			match = Type.toInt(ltok.nextToken());
			mismatch = Type.toInt(ltok.nextToken());
			repmatch = Type.toInt(ltok.nextToken());
			ns = Type.toInt(ltok.nextToken());
			Q_gapCount = Type.toInt(ltok.nextToken());
			Q_gapBases = Type.toInt(ltok.nextToken());
			T_gapCount = Type.toInt(ltok.nextToken());
			T_gapBases = Type.toInt(ltok.nextToken());
			strand = ltok.nextToken();
			Q_name = ltok.nextToken();
			Q_size = Type.toInt(ltok.nextToken());
			Q_start = Type.toInt(ltok.nextToken());
			Q_end = Type.toInt(ltok.nextToken());
			T_name = ltok.nextToken();
			T_size = Type.toInt(ltok.nextToken());
			T_start = Type.toInt(ltok.nextToken());
			T_end = Type.toInt(ltok.nextToken());
			blockCount = Type.toInt(ltok.nextToken());	
			temp_blockSizes = ltok.nextToken();
			temp_qStarts = ltok.nextToken();
			temp_tStarts = ltok.nextToken();
			percentMatch = (float) (Q_size - mismatch -(Q_size-match)) / Q_size;
			score = match + (repmatch/2) - mismatch - Q_gapCount - T_gapCount;
			overlaps = false;
			Overlaps_With = "";
		}
		
		//separate temp strings
		StringTokenizer btok = new StringTokenizer(temp_blockSizes, ",");
		while( btok.hasMoreTokens() ){
			blockSizes.add(btok.nextToken());
		}
		
		StringTokenizer qtok = new StringTokenizer(temp_qStarts, ",");
		while( qtok.hasMoreTokens() ){
			qStarts.add(qtok.nextToken());
		}
		
		StringTokenizer ttok = new StringTokenizer(temp_tStarts, ",");
		while( ttok.hasMoreTokens() ){
			tStarts.add(ttok.nextToken());
		}							
	}
	
	// interface methods
	
	public int get_match(){
		return match;
	}

	public int get_mismatch(){
		return mismatch;
	}

	public int get_repmatch(){
		return repmatch;
	}
	
	public int get_ns(){
		return ns;
	}
	
	public int get_Q_gapCount(){
		return Q_gapCount;
	}
	
	public int get_Q_gapBases(){
		return Q_gapBases;
	}
	
	public int get_T_gapCount(){
		return T_gapCount;
	}
	
	public int get_T_gapBases(){
		return T_gapBases;
	}
	
	public String get_strand(){
		return strand;
	}
	
	public String get_Q_name(){
		return Q_name;
	}
	
	public int get_Q_size(){
		return Q_size;
	}
	
	public int get_Q_start(){
		return Q_start;
	}
	
	public int get_Q_end(){
		return Q_end;
	}
	
	public String get_T_name(){
		return T_name;
	}
	
	public int get_T_size(){
		return T_size;
	}
	
	public int get_T_start(){
		return T_start;
	}
	
	public int get_T_end(){
		return T_end;
	}
	
	public int get_blockCount(){
		return blockCount;
	}

	public Vector get_blockSizes(){
		return blockSizes;
	}
	
	public int get_blockSize(int i){
		return Type.toInt( (String) (blockSizes.get(i) ));
	}
	
	public int get_qStart(int i){
		return Type.toInt( (String) (qStarts.get(i) ));
	}
	
	public int get_tStart(int i){
		return Type.toInt( (String) (tStarts.get(i) ));
	}
	
	public Vector get_qStarts(){
		return qStarts;
	}
	
	public Vector get_tStarts(){
		return tStarts;
	}
	
	public float get_percentMatch(){
		return percentMatch;	
	}
	
	public int get_score(){
		return score;
	}
	
	public boolean get_overlaps() {
		return overlaps;
	}
	
	public String get_Overlaps_With(){
		return Overlaps_With;
	}
			
	public void set_match(int i){
		match = i;
	}

	public void set_mismatch(int i){
		mismatch = i;
	}

	public void set_repmatch(int i){
		repmatch = i;
	}
	
	public void set_Q_gapCount(int i){
		Q_gapCount = i;
	}
	
	public void set_Q_gapBases(int i){
		Q_gapBases = i;
	}
	
	public void set_T_gapCount(int i){
		T_gapCount = i;
	}
	
	public void set_T_gapBases(int i){
		T_gapBases = i;
	}
	
	public void set_strand(String s){
		strand = s;
	}
	
	public void set_Q_name(String s){
		Q_name = s;
	}
	
	public void set_Q_size(int i){
		Q_size = i;
	}
	
	public void set_Q_start(int i){
		Q_start = i;
	}
	
	public void set_Q_end(int i){
		Q_end = i;
	}
	
	public void get_T_name(String s){
		T_name = s;
	}
	
	public void set_T_size(int i){
		T_size = i;
	}
	
	public void set_T_start(int i){
		Q_start = i;
	}
	
	public void set_T_end(int i){
		T_end = i;
	}
	
	public void set_blockCount(int i){
		blockCount = i;
	}

	public void set_blockSizes(Vector v){
		blockSizes = new Vector(v);
	}
	
	public void set_qStarts(Vector v){
		qStarts = new Vector(v);
	}
	
	public void set_tStarts(Vector v){
		tStarts = new Vector(v);
	}
	
	public void set_percentMatch(float f){
		percentMatch = f;	
	}
	
	public void set_score(int i){
		score = i;
	}
	
	public void set_overlaps(boolean b) {
		overlaps = b;
	}
	
	public void set_Overlaps_With(String s){
		Overlaps_With = s;
	}
	
	// to string
	public String toString(){
		String s;
		String t_b = "";
		String t_q = "";
		String t_t = ""; 
		
		for(int i=0; i < blockSizes.size(); i++){
			t_b = t_b + blockSizes.get(i) + ",";
		}
		
		for(int i=0; i < qStarts.size(); i++){
			t_q = t_q + qStarts.get(i) + ",";
		}
		
		for(int i=0; i < tStarts.size(); i++){
			t_t = t_t + tStarts.get(i) + ",";
		}
		
		
		s= match + " " + mismatch + " " + repmatch + " " + ns + " " + Q_gapCount + " " + Q_gapBases + 
		" " + T_gapCount + " " + T_gapBases + " " + strand + " " + Q_name + " " + Q_size + " " + Q_start +
		" " + Q_end + " " + T_name + " " + T_size + " " + T_start + " " + T_end + " " + blockCount +
		" " + t_b + " " + t_q + " " + t_t + " " + percentMatch + " " + score + " " + overlaps +
		" " + Overlaps_With;
		return s;
	}
	
	// methods
	
	public boolean overlaps(psl3 p){
		boolean ov = false;
		
		if(!T_name.equals(p.get_T_name())){
			return ov;
		}
		
		if( (T_start >= p.get_T_start()) && (T_start <= p.get_T_end()) ){
			overlaps = true;
			ov = true;
			Overlaps_With = p.get_Q_name();
		}
		if( (T_end >= p.get_T_start()) && (T_end <= p.get_T_end()) ){
			overlaps = true;
			ov = true;
			Overlaps_With = p.get_Q_name();
		}	
		
		if( (p.get_T_start() >= T_start ) && (p.get_T_start() <= T_end)){
			overlaps = true;
			ov = true;
			Overlaps_With = p.get_Q_name();
		}
		
		if( (p.get_T_end() >= T_start ) && (p.get_T_end() <= T_end)){
			overlaps = true;
			ov = true;
			Overlaps_With = p.get_Q_name();
		}
		return ov;
	}
	
	public String toGff(String source, String feature){
		String outstring = "";
		String temp_string = "";
		int temp_tend = 0;
		int temp_qend = 0;
		for(int i = 0; i < blockCount; i++){
			temp_tend = Type.toInt( (String) tStarts.get(i)) + Type.toInt( (String) blockSizes.get(i));
			temp_qend = Type.toInt( (String) qStarts.get(i)) + Type.toInt( (String) blockSizes.get(i));
			temp_string = Q_name + "\t" + source + "\t" + feature + "\t" + tStarts.get(i) + "\t" + temp_tend + "\t" + score + "\t" + strand + "\t" + "." + "\t" + Q_name + "\t" + qStarts.get(i) + "\t" + temp_qend; 
			outstring = outstring + temp_string;
			outstring = outstring + "\n";

			temp_string = "";
		}
		// System.out.println(outstring);
		return outstring;
	}
	
	public void set(psl3 p){
		match = p.get_match();
		mismatch = p.get_mismatch();
		repmatch = p.get_repmatch();
		ns = p.get_ns();
		Q_gapCount = p.get_Q_gapCount();
		Q_gapBases = p.get_Q_gapBases();
		T_gapCount = p.get_T_gapCount();
		T_gapBases = p.get_T_gapBases();
		strand = p.get_strand();
		Q_name = p.get_Q_name();
		Q_size = p.get_Q_size();
		Q_start = p.get_Q_start();
		Q_end = p.get_Q_end();
		T_name = p.get_T_name();
		T_size = p.get_T_size();
		T_start = p.get_T_start();
		T_end = p.get_T_end();
		blockCount = p.get_blockCount();
		Vector tb = new Vector(p.get_blockSizes());
		Vector tq = new Vector(p.get_qStarts());
		Vector tt = new Vector(p.get_tStarts());
		this.blockSizes.clear();
		this.qStarts.clear();
		this.tStarts.clear();
		
		for(int i=0; i < tb.size(); i++){
			this.blockSizes.add(tb.get(i));
		}
		for(int i=0; i < tq.size(); i++){
			this.qStarts.add(tq.get(i));
		}
		for(int i=0; i < tt.size(); i++){
			this.tStarts.add(tt.get(i));
		}
		percentMatch = p.get_percentMatch();
		score = p.get_score();
		overlaps = p.get_overlaps();
		Overlaps_With = p.get_Overlaps_With();
	}
	
	public String export(){
		String s;
		String t_b = "";
		String t_q = "";
		String t_t = ""; 
		
		for(int i=0; i < blockSizes.size(); i++){
			t_b = t_b + blockSizes.get(i) + ",";
		}
		
		for(int i=0; i < qStarts.size(); i++){
			t_q = t_q + qStarts.get(i) + ",";
		}
		
		for(int i=0; i < tStarts.size(); i++){
			t_t = t_t + tStarts.get(i) + ",";
		}
	
		s= match + "\t" + mismatch + "\t" + repmatch + "\t" + ns + "\t" + Q_gapCount + "\t" + Q_gapBases + 
		"\t" + T_gapCount + "\t" + T_gapBases + "\t" + strand + "\t" + Q_name + "\t" + Q_size + "\t" + Q_start +
		"\t" + Q_end + "\t" + T_name + "\t" + T_size + "\t" + T_start + "\t" + T_end + "\t" + blockCount +
		"\t" + t_b + "\t" + t_q + "\t" + t_t + "\n";
		
		return s;
	}

	public String print_header(){
		return "psLayout version 3" + "\n\n" + "match	mis- 	rep. 	N's	Q gap	Q gap	T gap	T gap	strand	Q        	Q   	Q    	Q  	T        	T   	T    	T  	block	blockSizes 	qStarts	 tStarts" + "\n" + "match	match	   	count	bases	count	bases	      	name     	size	start	end	name     	size	start	end	count" + "\n" + "---------------------------------------------------------------------------------------------------------------------------------------------------------------" + "\n";
  
	}
}


